<?php
namespace App\Models;

use Illuminate\Support\Str;
use Illuminate\Pagination\LengthAwarePaginator;
use App\UserModel;
use App\LoginModel;

class User
{
    /**
     * 注册用户
     * 
     * @author 谢望鑫 <xiewxin@addcn.com>
     * @param $param['phone'] 账号
     * @param $param['password'] 密码
     * @param $param['nickname'] 昵称
     * 
     * $return $userid 新增用户id
     */
    public function doRegister($param)
    {
        $data['phone'] = htmlentities($param['phone']);
        $data['password'] = md5(htmlentities($param['password']));
        $data['nickname'] = urlencode(htmlentities($param['nickname']));
        $data['createtime'] = time();
        $data['status'] = 1;
        
        $userid = UserModel::insertGetId($data);
        
        if ($userid) {
            return array('userid'=>$userid, 'createtime'=>$data['createtime']);
        } else {
            return array('userid'=>$userid);
        }
    }
    
    /**
     * 查看账户是否存在 
     * 
     * @author 谢望鑫 <xiewxin@addcn.com>
     * @param $phone 账号
     * 
     * $return $user 用户数据
     */
    public function userPhoneQuery($phone)
    {
        $user = UserModel::select('id',
            'usertype',
            'nickname',
            'avatar',
            'phone',
            'messagecount',
            'last_login_time',
            'createtime',
            'status'
            )
        ->where([
            ['phone', '=', htmlentities($phone)],
            ['status', '<>', 0],
        ])
        ->first();
        
        return $user;
    }
    
    /**
     * 校验用户密码
     * 
     * @author 谢望鑫 <xiewxin@addcn.com>
     * @param $param['phone'] 账号
     * @param $param['password'] 密码
     * 
     * $return $user 用户数据
     */
    public function loginCheck($param)
    {
        $user = UserModel::select('id',
            'usertype',
            'nickname',
            'avatar',
            'phone',
            'messagecount',
            'createtime',
            'status'
            )
            ->where([
                ['phone', '=', htmlentities($param['phone'])],
                ['password', '=', md5(htmlentities($param['password']))],
                ['status', '<>', 0],
            ])
            ->first();
            
         return $user;
    }
    
    /**
     * 登录
     * 
     * @author 谢望鑫 <xiewxin@addcn.com>
     * @param $param['phone'] 账号
     * @param $param['logintype'] 登录类型，1-pc登录，2-h5移动登录，3-安卓登录，4-ios登录
     * 
     * $return $user 用户数据
     */
    public function login($param)
    {
        $newtime = time();
        
        //将之前的登录记录设置为退出,pc无需
        if ($param['logintype'] != 1) {
            LoginModel::where([
                ['logintype', '=', $param['logintype']],
                ['phone', '=', htmlentities($param['phone'])]
            ])
            ->update(['status' => 0, 'statustime'=>$newtime]);
        }
        
        //新增登录记录
        $data['userid'] = $param['userid'];
        $data['phone'] = htmlentities($param['phone']);
        $data['accesstoken'] = (string) Str::uuid();
        $data['logintype'] = $param['logintype'];
        $data['createtime'] = $newtime;
        $data['status'] = 1;
        
        $loginid = LoginModel::insertGetId($data);
        if ($loginid) {
            UserModel::where([
                ['phone', '=', htmlentities($param['phone'])]
            ])
            ->update(['last_login_time' => $newtime]);
            $data['last_login_time'] = $newtime;
            
            unset($data['userid'], $data['phone'], $data['createtime'], $data['status']);
            
            return $data;
        }
        
        return false;
    }
    
    /**
     * 退出
     * 
     * @author 谢望鑫 <xiewxin@addcn.com>
     * @param $param['phone'] 账号
     * @param $param['accesstoken'] 操作令牌
     * @param $param['logintype'] 登录类型，1-pc登录，2-h5移动登录，3-安卓登录，4-ios登录
     * 
     * $return $b 
     */
    public function logout($param)
    {
        $newtime = time();
        
        $where = array();
        $where[]=['phone', '=', $param['phone']];
        
        if ($param['logintype'] != 1){
            $where[] = ['logintype', '=', $param['logintype']];
        }else {
            $where[] = ['accesstoken', '=', $param['accesstoken']];
        }
       
        $b = LoginModel::where($where)
        ->update(['status' => 0, 'statustime' => $newtime]);
        
        return $b;
    }
    
    /**
     * 令牌校验
     * 
     * @author 谢望鑫 <xiewxin@addcn.com>
     * @param $param['phone'] 账号
     * @param $param['accesstoken'] 操作令牌
     * 
     * $return $user 用户数据
     */
    public function getToken($param)
    {
        $user = LoginModel::select('user_login.id',
            'user_login.userid',
            'user_login.phone',
            'user_login.accesstoken',
            'user_login.logintype',
            'user_login.createtime as logintime',
            'user_login.status as loginstatus',
            'user.usertype',
            'user.nickname',
            'user.avatar',
            'user.messagecount',
            'user.more',
            'user.last_login_time',
            'user.createtime',
            'user.status'
            )
            ->leftJoin('user', 'user_login.userid', '=', 'user.id')
            ->where([
                ['user_login.phone', '=', $param['phone']],
                ['user_login.accesstoken', '=', $param['accesstoken']],
                ['user_login.status', '=', 1],
            ])
            ->first();
            
            return $user;
    }
    
    /**
     * 设置黑名单
     * 
     * @author 谢望鑫 <xiewxin@addcn.com>
     * @param $param['userid'] 用户id
     * @param $param['status'] 黑名单状态
     * 
     * $return $b
     */
    public function blacklistSetting($param)
    {
        $newtime = time();
        
        $b = UserModel::where([
            ['usertype', '<>', 1],
            ['id', '=', $param['userid']],
            ['status', '<>', $param['status']]
        ])
        ->update(['status' => $param['status'], 'statustime' => $newtime]);
        
        return $b;
    }
    
    /**
     * 用户列表
     * 
     * @author 谢望鑫 <xiewxin@addcn.com>
     * @param $param['nickname'] 用户id
     * @param $param['status'] 黑名单状态
     * @param $param['startime'] 创建开始时间
     * @param $param['endtime'] 结束时间
     * 
     * $return $userData
     */
    public function userList($param)
    {
        $newtime = time();
        $where = array();
        $where[] = ['usertype', '<>', 1];
        $where[] = ['status', '<>', 0];
        
        if (!empty($param['startime'])) {
            $where[] = ['createtime', '>=', (int)$param['startime']];
        }
        
        if (!empty($param['endtime'])) {
            $where[] = ['createtime', '<=', (int)$param['endtime']];
        }
        
        if (!empty($param['status'])) {
            $where[] = ['status', '=', (int)$param['status']];
        }
       
        $userData = UserModel::select('id',
            'usertype',
            'nickname',
            'avatar',
            'phone',
            'messagecount',
            'last_login_time',
            'createtime',
            'status'
            )
        ->where($where)
        ->orderBy('createtime', 'desc');
        
        if (!empty($param['nickname'])) {
            $nickname = urlencode(htmlentities($param['nickname']));
            $orwhere = "(`nickname`='$nickname' or `phone`='$nickname')";
            $userData = $userData->whereRaw($orwhere);
        }
        
        return $userData->paginate(10);
    }
}